package com.yy.xshell.process.resultHandler;

import com.alibaba.fastjson2.JSONObject;
import com.yy.xshell.core.ShellContext;
import com.yy.xshell.process.Handler;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 处理颜色
 * @author wxy
 * @version 1.0
 * @date 2024/8/21 10:00
 */
public class StyleHandler extends Handler<ShellContext> {

    final static String REG =  "(\\u001b\\[(\\d{1,2})?([;])?(\\d{1,2})?(m))";
    final static String REG_EMPTY = "[ ]+";
    final static String REG_EMPTY_DIV =  "<div style='[a-zA-Z0-9: ;\\-]{1,}'>(</div>)";
    //20px
    ShellContext.Environment environment = null;

    @Override
    public ShellContext handler(ShellContext shellContext) {
        environment = shellContext.getEnvironment();
        String msg = shellContext.getMsg();
        StringBuilder append = new StringBuilder();
        String[] split = msg.split("\\n");
        for(String block : split){
            append.append("<div>");
            if(block.contains("\u001b") || block.contains(" ")){
                String s = processEmpty(block);
                String str = findStr(s);
                append.append(str);
            }else {
                append.append(block);
            }
            append.append("</div>");
        }
        shellContext.setMsg(append.toString());
        return shellContext;
    }

    @Override
    public boolean accept(ShellContext result) {
//        Pattern pattern = Pattern.compile(REG, Pattern.CASE_INSENSITIVE);
//        Matcher matcher = pattern.matcher(result.getString("msg"));
//        return matcher.find();
        return true;
    }

    private String findStr(String s1) {
        Pattern pattern = Pattern.compile(REG, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(s1);
        StringBuilder stringBuilder = new StringBuilder();
        int end = 0;
        String style = "";
        while (matcher.find()) {
            if (end != matcher.start()) {
                findStrProcess(stringBuilder, style, s1, end, matcher.start());
            }
            style = processBlock(s1.substring(matcher.start(), matcher.end()));
            end = matcher.end();
        }
        if(end < s1.length()-1){
            findStrProcess(stringBuilder, style, s1, end, s1.length());
        }
        return stringBuilder.toString();
    }


    private void findStrProcess(StringBuilder stringBuilder, String style, String s1, int start,int end){
        String content = s1.substring(start, end);
        if (StringUtils.isNotEmpty(style)) {
            stringBuilder.append(String.format("<div style='text-align-last: justify;display: inline-block;width: %dpx;", content.length() * (environment.getFontSize() / 2)));
            stringBuilder.append(style);
            stringBuilder.append("'>");
            stringBuilder.append(content);
            stringBuilder.append("</div>");
        }else {
            stringBuilder.append(content);
        }

    }

    private String processBlock(String blockStr){
        if(StringUtils.isEmpty(blockStr) || blockStr.length() < 8){
            return "";
        }
        blockStr =  blockStr.substring(2, blockStr.length()-1);
        String[] split = blockStr.split(";");
        StringBuilder style = new StringBuilder();
        for(String s : split){
            if(s.equals("0")){
                style = new StringBuilder();
            }else if("1".equals(s) || "01".equals(s)){
                //加粗文本
//                style.append("font-weight: bold;");
            }else if("2".equals(s) || "02".equals(s)){
                //弱化
                style.append("opacity: 0.8；");
            }else if("3".equals(s) || "03".equals(s)){
                //斜体文本
                style.append("font-style: italic;");
            }else if("4".equals(s) || "04".equals(s)){
                //下划线
                style.append("text-decoration: underline;");
            }else if("5".equals(s) || "05".equals(s)){
                //缓慢闪烁
                style.append("animation: blink 1s infinite;");
            }else if("6".equals(s) || "06".equals(s)){
                //快速闪烁
                style.append("animation: blink 0.3s infinite;");
            }else if("7".equals(s) || "07".equals(s)){
                //返显示 交换前景色背景色
                style.append("filter: invert(50%);");
            }else if("8".equals(s) || "08".equals(s)){
                //隐藏
                style.append("display: none;");
            }else if("9".equals(s) || "09".equals(s)){
                //划除
                style.append("text-decoration: line-through;");
            }else if("10".equals(s)){
                //默认字体
                // style.append("text-decoration: line-through;");
                //11-20替代字体
                //21-29 关闭前面设置某些属性
            }
            else if("30".equals(s)){
                //黑色
                style.append("color: black;");
            }
            else if("31".equals(s)){
                style.append("color: red;");
            }
            else if("32".equals(s)){
                style.append("color: green;");
            }
            else if("33".equals(s)){
                style.append("color: yellow;");
            }
            else if("34".equals(s)){
                //style.append("color: blue;");
                style.append("color: #50BEFF;");
            }
            else if("35".equals(s)){
                style.append("color: fuchsia;");
            }
            else if("36".equals(s)){
                style.append("color: aqua;");
            }
            else if("37".equals(s)){
                style.append("color: white;");
            }

            else if("40".equals(s)){
                style.append("background-color: black;");
            }

            else if("41".equals(s)){
                style.append("background-color: red;");
            }
            else if("42".equals(s)){
                style.append("background-color: green;");
            }
            else if("43".equals(s)){
                style.append("background-color: yellow;");
            }
            else if("44".equals(s)){
                style.append("background-color: blue;");
            }
            else if("45".equals(s)){
                style.append("background-color: fuchsia;");
            }
            else if("46".equals(s)){
                style.append("background-color: aqua;");
            }
            else if("47".equals(s)){
                style.append("background-color: white;");
            }
        }
        return style.toString();
    }


    private String processEmpty(String s){
        StringBuilder result = new StringBuilder();
        Pattern pattern = Pattern.compile(REG_EMPTY, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(s);
        int end = 0;
        while (matcher.find()){
            if(matcher.start() != end){
                result.append(s.substring(end, matcher.start()));
            }
            String empty = s.substring(matcher.start(), matcher.end());
            result.append(String.format("<div style='width: %dpx;display: inline-block;'></div>", empty.length() * (environment.getFontSize() / 2)));
            end = matcher.end();
        }
        if(end < s.length() - 1){
            result.append(s.substring(end));
        }
        return result.toString();
    }

    public static void main(String[] args) {
        String a = "\u001B[0m\u001B[01;32mclear.sh\u001B[0m                           \u001B[01;32mteamPartner.jar\u001B[0m\n";
//        Pattern pattern = Pattern.compile(REG, Pattern.CASE_INSENSITIVE);
//        Matcher matcher = pattern.matcher(a);
//        System.out.println(matcher.find());
//        String empty = processEmpty(a);
//        String end = findStr(empty);
//        System.out.println(end);
    }
}
